<?php
namespace haibao\erp\business;

use haibao\erp\business\usergroup\UserGroup as UserGroup;

abstract class Base{
	const ACCESS_DENIED = 1000;
	const DATA_EMPTY = 10000;
	const DATA_EMPTY_NAME = 10001;
	const DATA_EMPTY_PASSWORD = 10002;
	const DATA_EMPTY_ID = 10003;
	const DATA_EMPTY_USER_GROUP = 10004;
	const DATA_EMPTY_REAL_NAME = 10005;
	const DATA_EMPTY_FUNCTION_CODE =10006;
	const DATA_EMPTY_EVNET_TAG = 10007;
	const DATA_EMPTY_EVENT_KEYWORDS = 10008;
	const DATA_EMPTY_PARENT_ID  = 10009;
	
	const DATA_EMPTY_PAGE_CONTROL_CATEGORY_ID = 10100;
	const DATA_EMPTY_PAGE_CONTROL_CATEGORY_NAME = 10101;
	const DATA_EMPTY_PAGE_CONTROL_CATEGORY_PARENT_ID = 10102;
	const DATA_EMPTY_PAGE_CONTROL_NAME = 10103;
	const DATA_EMPTY_PAGE_CONTROL_CONTROLLER = 10104;
	const DATA_EMPTY_PAGE_CONTROL_CATEGORY = 10105;
	const DATA_EMPTY_PAGE_CONTROL_CONTENT = 10106;
	const DATA_EMPTY_PAGE_CONTROL_ID = 10107;
	
	const DATA_EMPTY_FEATURE_ID = 10203;
	const DATA_EMPTY_FEATURE_URL = 10200;
	const DATA_EMPTY_FEATURE_NAME = 10201;
	const DATA_EMPTY_FEATURE_CONTENT = 10202;
	const DATA_EMPTY_FEATURE_SERIAL_NAME = 10300;
	const DATA_EMPTY_FEATURE_SERIAL_ID = 10301;
	
	const DATA_ERROR = 20000;
	const DATA_ERROR_NAME = 20001;
	const DATA_ERROR_PASSWORD = 20002;
	const DATA_ERROR_NAME_EXIST = 20003;
	const DATA_ERROR_EMAIL_EXIST = 20004;
	const DATA_ERROR_NEED_CODE = 20005;
	const DATA_ERROR_CAPTCHA = 20006;
	
	const DATA_ERROR_PAGE_CONTROL_CATEGORY_NAME_EXIST = 20103;
	const DATA_ERROR_PAGE_CONTROL_NAME_EXIST = 20104;
	const DATA_ERROR_PAGE_CONTROL_CATEGORY_CAN_NOT_DELETE = 20105;
	
	const DATA_ERROR_FEATURE_URL_EXIST = 20200;
	const DATA_ERROR_FEATURE_NAME_EXIST = 20201;
	const DATA_ERROR_FEATURE_SERIAL_NAME_EXIST = 20202;

	const FUNCTION_CODE = 110;
	
	const FUNC_CODE_USER_ADD = 10000;
	const FUNC_CODE_USER_LOGIN = 10001;
	const FUNC_CODE_USER_LOGOUT = 10002;
	const FUNC_CODE_USER_EDIT = 10003;
	const FUNC_CODE_USER_LIST = 10004;
	
	const FUNC_CODE_USERGROUP_ADD = 10005;
	const FUNC_CODE_USERGROUP_EDIT = 10006;
	const FUNC_CODE_USERGROUP_DELTE = 10007;
	const FUNC_CODE_USERGROUP_SAVE = 10008;
	const FUNC_CODE_USERGROUP_LIST = 10009;
	const FUNC_CODE_USERGROUP_UNLOCK = 10010;
	const FUNC_CODE_USER_CHANGE_PASSWORD = 10011;
	const FUNC_CODE_USER_SOCIAL_TIME = 10012;
	
	const FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD = 10201;
	const FUNC_CODE_PAGE_CONTROL_CATEGORY_EDIT = 10202;
	const FUNC_CODE_PAGE_CONTROL_CATEGORY_DELETE = 10203;
	const FUNC_CODE_PAGE_CONTROL_CATEGORY_LIST = 10208;
	const FUNC_CODE_PAGE_CONTROL_ADD = 10204;
	const FUNC_CODE_PAGE_CONTROL_EDIT = 10205;
	const FUNC_CODE_PAGE_CONTROL_DELETE = 10206;
	const FUNC_CODE_PAGE_CONTROL_LIST = 10207;

	const FUNC_CODE_PAGE_CONTROL_ADD_DATA = 10301;
	const FUNC_CODE_PAGE_CONTROL_EDIT_DATA = 10302;
	const FUNC_CODE_PAGE_CONTROL_DEL_DATA = 10303;
	
	//网站管理
	const FUNC_CODE_WEB_LIST = 10304;
	const FUNC_CODE_WAP_LIST = 10305;

	
	//公共导航
	const FUNC_CODE_PAGE_CONTROL_MANAGER_DATA_45 = 11045;
	
	//异地上下班打卡
	const FUNC_CODE_OTHER_WORKSIGN = 111207;//异地上下班打卡
	
	//岗位
	const FUNC_CODE_JOB_ADD = 119110;
	const FUNC_CODE_JOB_LIST = 119111;
	const FUNC_CODE_JOB_EDIT = 119112;
	const FUNC_CODE_JOB_DEL = 119113;
	const FUNC_CODE_JOB_CATEGORY_ADD = 119114;
	const FUNC_CODE_JOB_CATEGORY_LIST = 119115;
	const FUNC_CODE_JOB_CATEGORY_EDIT = 119116;
	const FUNC_CODE_JOB_CATEGORY_DEL = 119117;
	
	//考勤管理
	const FUNC_CODE_ATTENDANCE_IMPORT = 120000;
	const FUNC_CODE_ATTENDANCE_MANAGE = 120001;
	const FUNC_CODE_ATTENDANCE_EDIT = 120002;
	const FUNC_CODE_ATTENDANCE_APPROVE = 120003;
	const FUNC_CODE_ATTENDANCE_WORK_DAY_ADD = 120004;
	const FUNC_CODE_ATTENDANCE_WORK_DAY_LIST = 120005;
	const FUNC_CODE_ATTENDANCE_WORK_DAY_EDIT = 120006;
	const FUNC_CODE_ATTENDANCE_WORK_DAY_DEL = 120007;
	const FUNC_CODE_ATTENDANCE_WORK_CYCLE_ADD = 120008;
	const FUNC_CODE_ATTENDANCE_WORK_CYCLE_EDIT = 120009;
	const FUNC_CODE_ATTENDANCE_WORK_CYCLE_LIST = 120010;
	const FUNC_CODE_ATTENDANCE_WORK_CYCLE_DEL = 120011;
	const FUNC_CODE_ATTENDANCE_REQUEST_LIST = 120012;//申请列表（自己）
	const FUNC_CODE_ATTENDANCE_REQUEST_AUDIT_LIST = 120013;//申请批准
	const FUNC_CODE_ATTENDANCE_USER_LIST = 120014;
	const FUNC_CODE_ATTENDANCE_USER_ADD = 120015;
	const FUNC_CODE_ATTENDANCE_USER_UPDATE = 120016;
	const FUNC_CODE_ATTENDANCE_USER_DEL = 120017;
	CONST FUNC_CODE_ATTENDANCE_DELETE = 120018;//考勤记录删除(上级领导)
	const FUNC_CODE_ATTENDANCE_STATISTICS = 120019;//考勤统计
	const FUNC_CODE_ATTENDANCE_HISTORY = 120020;//考勤原打卡记录查询
	const FUNC_CODE_ATTENDANCE_ALL_REQUEST_LIST = 120021;
	const FUNC_CODE_ATTENDANCE_HR_MANAGE = 120022;
	const FUNC_CODE_ATTENDANCE_REQUEST_LIST_HR = 120023;//申请记录HR	
	const FUNC_CODE_ATTENDANCE_APPROVE_LIST_HR = 120024;//考勤状态
	
	const DATA_ERROR_CODE_PERMISION_SHOW_HISTORY = 10005;
	const DATA_ERROR_CODE_SYSTEM = 40001;//系统错误
	
	
	/* 单品类别管理 */
	const FUNC_CODE_PRODUCT_CATEGORY_LIST = 100301;
	const FUNC_CODE_PRODUCT_CATEGORY_ADD = 100302;
	const FUNC_CODE_PRODUCT_CATEGORY_SAVE = 100303;
	const FUNC_CODE_PRODUCT_CATEGORY_DELETE = 100304;
	/* 单品属性 */
	const FUNC_CODE_PRODUCT_PROPERTY_LIST = 100305;
	const FUNC_CODE_PRODUCT_PROPERTY_ADD = 100306;
	const FUNC_CODE_PRODUCT_PROPERTY_DEL = 100307;
	const FUNC_CODE_PRODUCT_PROPERTY_EDIT = 100308;
	/* 单品管理 */
	const FUNC_CODE_PRODUCT_LIST = 100309;
	const FUNC_CODE_PRODUCT_ADD = 100310;
	const FUNC_CODE_PRODUCT_DEL = 100311;
	const FUNC_CODE_PRODUCT_EDIT = 100312;
    const FUNC_CODE_PRODUCT_ADD_FROM_SPIDER = 100313;
	
	/* 品牌管理 */
	const FUNC_CODE_BRAND_LIST = 101101;
	const FUNC_CODE_BRAND_ADD = 101102;
	const FUNC_CODE_BRAND_EDIT = 101103;
	const FUNC_CODE_BRAND_DELETE = 101104;
	/*图片管理*/
    const FUNC_CODE_IMAGE_LIST = 100400;

	
	private static $currentUser = null;
	
	public function __construct(){}
	
	public static function allowOperation($funcCode,$userPermission=\haibao\erp\model\data\user\User::PERMISSION_EMPLOYEE,$userId=null){
	    $userModel = null;
		if ($userId){
			$userModel = \haibao\erp\business\user\User::getUserById($userId);
			if (!$userModel){
				return false;
			}
		}
		else{
			$userModel = self::getCurrentUser();
		}
		$allow = false;
		if($userModel){
			if ($userModel->Permission == \haibao\erp\model\data\user\User::PERMISSION_ADMIN){
				return true;
			}
			
			switch ($userPermission){
				case \haibao\erp\model\data\user\User::PERMISSION_EMPLOYEE:
					break;
				case \haibao\erp\model\data\user\User::PERMISSION_MANAGER:
					if ($userModel->Permission == \haibao\erp\model\data\user\User::PERMISSION_EMPLOYEE){
						return false;
					}
					break;
				case \haibao\erp\model\data\user\User::PERMISSION_ADMIN:
					if ($userModel->Permission != \haibao\erp\model\data\user\User::PERMISSION_ADMIN){
						return false;
					}
					break;
			}
			
			foreach($userModel->Groups as $groupId){
				$funcCodes = UserGroup::getFuncCodesByGroupId($groupId);
				if (in_array($funcCode, $funcCodes)){
					$allow = true;
					break;
				}
			}
		}
		
		return $allow;
	}
	
	public static function checkOperation($funcCode,$userPermission=\haibao\erp\model\data\user\User::PERMISSION_EMPLOYEE,$userId=null){
		if (!self::allowOperation($funcCode,$userPermission,$userId)){
			throw new BusinessException('对不起，你无此权限，请联系系统管理员！',self::ACCESS_DENIED);
		}
	}
	
	public static function setCurrentUser($userModel){
		self::$currentUser = $userModel;
	}
	
	public static function getCurrentUser(){
		return self::$currentUser;
	}
	
	/**
	 * 保存生图片
	 */
	public static function generateImage($path, $stream){
	    $savePath = self::getSaveImagePath($path);
	    self::createDir($savePath);
	    file_put_contents($savePath, $stream, LOCK_EX);
	}
	
	/**
	 * 获取文件保存路径
	 */
	public static function getSaveImagePath($path){
	    $path = '/'. ltrim($path,'/');
	    return \haibao\frame\Environment::path() . $path;
	}
	
	/**
	 * 创建文件保存路径
	 */
	public static function createDir($path){
	    $path = pathinfo( $path );
	    return is_writable($path['dirname']) ?: mkdir($path['dirname'], 750, true);
	}
	
	
	public static function saveOperateHistory($operateType,$operateData,$userId = null){
		if (!$userId){
			$userId = self::getCurrentUser()->Id;
		}
		
		$model = new \haibao\erp\model\data\user\UserOperateHistory();
		$model->UserId = $userId;
		$model->OperateData = json_encode($operateData);
		$model->OperateType = $operateType;
		$model->OperateTime = new \DateTime();
		$data = new \haibao\erp\data\user\UserOperateHistory();
		
		$data->saveHistory($model);
	}
	
	/**
	 * 单品分类货币属性
	 */
	public static $monthProperty = array(
	    "1"=>"USD",
	    "2"=>"RMB",
	    "3"=>"EUR",
	    "4"=>"GBP",
	    "5"=>"HKD"
	);
}
